package com.nny.alarm.widget;


import com.nny.alarm.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ActionBar extends RelativeLayout implements View.OnClickListener
{
	ImageView mIcon;
	ImageView mBack;
	ViewGroup mContainer;
	ViewGroup mBackContainer;
	OnInitToolsListener mOnInitToolsListener;
	OnActionListener mOnActionListener;
	MOnInitToolsListener mDefaultInitToolsListenr;

	public static final int ACTION_BACK = -1;
	public static final int ACTION_MENU = -2;
	
	public static final int TOOLSLAYOUT_DEFAULT = -1;
	
	boolean mbShowBack = false;
	int mnToolsLayout = TOOLSLAYOUT_DEFAULT;
	CharSequence msTitle = "";
	int mnIcon = R.drawable.ic_launcher;
	
	
	public ActionBar(Context context)
	{
		super(context);
		initView(null);
	}

	public ActionBar(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initView(attrs);
	}
	
	public void setOnInitToolsListener(OnInitToolsListener l)
	{
		mOnInitToolsListener = l;
	}
	
	public void setOnActionListener(OnActionListener l)
	{
		mOnActionListener = l;
	}
	
	public void setToolsLayout(int resId)
	{
		mnToolsLayout = resId;
	}
	
	public void setTitle(CharSequence title)
	{
		msTitle = title;
		if(null != mDefaultInitToolsListenr.mTitle)
			mDefaultInitToolsListenr.mTitle.setText(title);
	}
	
	public CharSequence getTitle()
	{
		return msTitle;
	}
	
	public void setShowBack(boolean show)
	{
		mbShowBack = show;
		if(mBackContainer != null && mBack != null)
		{
			mBackContainer.setOnClickListener(mbShowBack ? this : null);
			mBackContainer.setClickable(mbShowBack);
			mBack.setVisibility(mbShowBack ? View.VISIBLE : View.GONE);
		}
	}
	
	private void initView(AttributeSet attrs)
	{
		final Context ctx = getContext();
		initAttrs(ctx, attrs);
		mDefaultInitToolsListenr = new MOnInitToolsListener();
		View.inflate(getContext(), R.layout.view_actionbar, this);	
	}
	
	private void initAttrs(Context ctx, AttributeSet attrs)
	{
		if(null == attrs)
			return;
		
		TypedArray arr = ctx.obtainStyledAttributes(attrs, R.styleable.ActionBar);
		mbShowBack = arr.getBoolean(R.styleable.ActionBar_ux_back, false);
		msTitle = arr.getText(R.styleable.ActionBar_ux_title);
		mnToolsLayout = arr.getResourceId(R.styleable.ActionBar_ux_content, TOOLSLAYOUT_DEFAULT);
		mnIcon = arr.getResourceId(R.styleable.ActionBar_ux_icon, R.drawable.ic_launcher);
		arr.recycle();
	}

	@Override
	protected void onFinishInflate()
	{
		super.onFinishInflate();
		
		final Context ctx = getContext();
		
		mContainer = (ViewGroup)findViewById(R.id.actionbar_ctn_op);
		mBackContainer = (ViewGroup)findViewById(R.id.actionbar_back);
		mIcon = (ImageView)findViewById(R.id.actionbar_iv_icon);
		mBack = (ImageView)findViewById(R.id.actionbar_iv_back);
		
		setShowBack(mbShowBack);
		initToolsLayout(ctx);
	}

	@Override
	public void onClick(View v)
	{
		if(null == mOnActionListener)
			return;

		int id = v.getId();
		if(id == R.id.actionbar_back)
		{
			mOnActionListener.onAction(this, v, ACTION_BACK);
		}
	}
	
	protected void initToolsLayout(final Context ctx)
	{		
		int id = (mnToolsLayout <= 0) ? R.layout.view_def_tools : mnToolsLayout;
		View.inflate(ctx, id, mContainer);
		
		if(mOnInitToolsListener != null)
		{
			mOnInitToolsListener.OnInitTools(this, mContainer, mnToolsLayout);
		}
		else if(mDefaultInitToolsListenr != null && mnToolsLayout <= 0)
		{
			mDefaultInitToolsListenr.OnInitTools(this, mContainer, mnToolsLayout);
		}
	}
	
	public void initTools()
	{
		mContainer.removeAllViews();
		initToolsLayout(getContext());
	}
	
	public interface OnInitToolsListener
	{
		public void OnInitTools(ActionBar bar, ViewGroup parent, int id);
	}
	
	public interface OnActionListener
	{
		public void onAction(ActionBar bar, View v, int action);
	}
	
	private class MOnInitToolsListener implements OnInitToolsListener
	{
		TextView mTitle;
		
		@Override
		public void OnInitTools(ActionBar bar, ViewGroup parent, int id)
		{
			if(parent == null)
				return;
			
			mTitle = (TextView)parent.findViewById(R.id.tools_title);
			
			if(null != mTitle && msTitle != null)
				mTitle.setText(msTitle);
		}	
	}
}
